package com.example.demo.controller;

import com.example.demo.dto.PieDto;
import com.example.demo.entity.*;
import com.example.demo.service.MarketService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.ws.ResponseWrapper;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/market")
public class MarketController {
    @Autowired
    private MarketService marketService;
    @RequestMapping("/show_market_page")
    public String show_market_page(Model model){
        int market_num;
        List<Market> markets=marketService.findMarket();
        List<MarketTypeInfo> marketTypeInfos=marketService.findMarketTypeInfo();
        List<MarketAreaInfo> marketAreaInfos=marketService.findMarketAreaInfo();
        List<MarketPointRank> marketPointRanks=marketService.findMarketPointRank();
        List<MarketPopularityRank> marketPopularityRanks=marketService.findMarketPopularityRank();

        //人均消费
        double market_average_money =0;
        double money=0;
        //平均评分
        double market_average_point=0;
        double points=0;
        //种类数量
        int market_type_num=0;
        //包含地区
        int market_area_num=0;
        //最火类型
        String market_hot_type=null;
        market_num=markets.size();
        for (int i=0;i<market_num;i++){
            Market market = markets.get(i);
            money=money+market.getAverage();
            points=points+market.getPoint();
        }
        market_average_money=money/market_num;
        BigDecimal a = new BigDecimal(market_average_money);
        market_average_money=a.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        market_average_point=points/market_num;
        BigDecimal b = new BigDecimal(market_average_point);
        market_average_point=b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        market_type_num=marketTypeInfos.size();
        market_area_num=marketAreaInfos.size();
        MarketTypeInfo marketTypeInfoMax=marketTypeInfos.get(0);
        for (int i=0;i<market_type_num;i++){
            MarketTypeInfo marketTypeInfo=marketTypeInfos.get(i);
            if (marketTypeInfo.getNum()>marketTypeInfoMax.getNum()){
                marketTypeInfoMax=marketTypeInfo;
            }
        }
        market_hot_type=marketTypeInfoMax.getType();
        model.addAttribute("market_num",market_num);
        model.addAttribute("market_average_money",market_average_money);
        model.addAttribute("market_average_point", market_average_point);
        model.addAttribute("market_type_num",market_type_num);
        model.addAttribute("market_area_num",market_area_num);
        model.addAttribute("market_hot_type",market_hot_type);
        model.addAttribute("marketPointRanks",marketPointRanks.subList(0,20));
        model.addAttribute("marketPopularityRanks",marketPopularityRanks.subList(0,20));
        return "market";
    }
    @RequestMapping("/marketInfo")
    public ModelAndView marketInfo(ModelAndView modelAndView,@RequestParam(defaultValue="1")Integer
            pageNum,@RequestParam(defaultValue="15")Integer pageSize){
        PageInfo<Market> marketPageInfo=marketService.selectMarketPage(pageNum, pageSize);
        modelAndView.addObject("marketPageInfo",marketPageInfo);
        modelAndView.setViewName("marketInfo");
        return modelAndView;
    }
    @RequestMapping("/market_echarts_1")
    @ResponseBody
    public List<PieDto> market_echarts_1(){
    List<PieDto> pieDtos = new ArrayList<>();
    List<MarketTypeInfo> marketTypeInfos=marketService.findMarketTypeInfo();
    for (int i=0;i<marketTypeInfos.size();i++){
        MarketTypeInfo marketTypeInfodb=marketTypeInfos.get(i);
        PieDto pieDtodb=new PieDto();
        pieDtodb.setName(marketTypeInfodb.getType());
        pieDtodb.setValue(marketTypeInfodb.getNum());
        pieDtos.add(pieDtodb);
    }
    return pieDtos;
    }
    @RequestMapping("/market_echarts_2_v")
    @ResponseBody
    public List<List<Double>> market_echarts_2_v(){
        List<List<Double>> vlist =new ArrayList<>();
        List<Double> list1=new ArrayList<>();
        List<Double> list2=new ArrayList<>();
        List<MarketTypeInfo> marketTypeInfos = marketService.findMarketTypeInfo();
        for (int i=0;i<marketTypeInfos.size();i++){
            MarketTypeInfo marketTypeInfo = marketTypeInfos.get(i);
            list1.add((double)marketTypeInfo.getNum());
            list2.add(marketTypeInfo.getAverage());
        }
        vlist.add(list1);
        vlist.add(list2);
        return vlist;
    }
    @RequestMapping("/market_echarts_2_x")
    @ResponseBody
    public List<String> market_echarts_2_x(){
        List<String> xlist=new ArrayList<>();
        List<MarketTypeInfo> marketTypeInfos=marketService.findMarketTypeInfo();
        for (int i=0;i<marketTypeInfos.size();i++){
            MarketTypeInfo marketTypeInfo =marketTypeInfos.get(i);
            xlist.add(marketTypeInfo.getType());
        }
        return xlist;
    }
    @RequestMapping("/market_echarts_3")
    @ResponseBody
    public List<PieDto> market_echarts_3(){
        List<PieDto> pieDtos=new ArrayList<>();
        List<MarketAreaInfo> marketAreaInfos=marketService.findMarketAreaInfo();
        for (int i=0;i<marketAreaInfos.size();i++){
            MarketAreaInfo marketAreaInfo=marketAreaInfos.get(i);
            PieDto pieDtodb=new PieDto();
            pieDtodb.setName(marketAreaInfo.getAreaName());
            pieDtodb.setValue(marketAreaInfo.getNum());
            pieDtos.add(pieDtodb);
        }
        return pieDtos;

    }
    @RequestMapping("/insertMarket")
    public String insertMarket(Model model){
        List<MarketAreaInfo> marketAreaInfos=marketService.findMarketAreaInfo();
        List<MarketTypeInfo> marketTypeInfos=marketService.findMarketTypeInfo();
        model.addAttribute("type",marketTypeInfos);
        model.addAttribute("area",marketAreaInfos);
        return "insertMarket";
    }
    @RequestMapping("/doinsertMarket")
    @ResponseBody
    public Map<String,Object> doinsertMarket(Market market){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        Market marketdb=marketService.findMarketByName(market.getName());
        if(marketdb!=null){
            resultMap.put("result","这家店已经存在啦");
        }else {
            marketService.insertMarket(market);
            resultMap.put("result","SUCCESS");
        }
        return resultMap;
    }
    @RequestMapping("/updateMarket")
    public String updateMarket(Model model,int id){
        Market market=marketService.findMarketById(id);
        List<MarketAreaInfo> marketAreaInfos=marketService.findMarketAreaInfo();
        List<MarketTypeInfo> marketTypeInfos=marketService.findMarketTypeInfo();
        model.addAttribute("type",marketTypeInfos);
        model.addAttribute("area",marketAreaInfos);
        model.addAttribute("market",market);
        return "updateMarket";
    }
    @RequestMapping("/doupdateMarket")
    @ResponseBody
    public Map<String,Object> doupdateMarket(Market market){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        Market marketdb=marketService.findMarketByName(market.getName());
        if (marketdb!=null&&marketdb.getId()!=market.getId()){
            resultMap.put("result","该家店已经存在啦");
        }else {
            Market market1=marketService.findMarketById(market.getId());
            marketService.updateMarketEX(market1);
            marketService.updateMarket(market);
            resultMap.put("result","SUCCESS");
        }
        return resultMap;
    }
    @RequestMapping("/deleteMarket")
    @ResponseBody
    public Map<String,Object> deleteMarket(int id){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        marketService.deleteMarket(id);
        resultMap.put("result","SUCCESS");
        return resultMap;
    }

}
